package net.dodoman.spider;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.junit.Test;

public class HtmlParserTester {

	private static final String MZT_URL = "http://www.gdmz.gov.cn";

	private static final String[] FILTERS = { "TABLE", "TBODY", "TR", "TD" };
	// private static final String[] FILTERS = { "TABLE" };

	private static final String KEY_WORD = "FCK__ShowTableBorders";

	/** leibie kaishi */
	private static final String LB_START_KEY = "ff0000>";

	/** leibie jieshu jieguo kaishi */
	private static final String LB_END_AND_JG_START_KEY = "</FONT>";

	/** jieguojieshu */
	private static final String JG_END_KEY = "</";

	@Test
	public void kjggList() {
		try {
			Parser parser = new Parser(MZT_URL);
			// 编码格式要根据网站html编码进行编码
			parser.setEncoding(ParserUtil.GB2312_CHARSET);
			// 按TagNameFilter获取Tag标签Html源码
			TagNameFilter filter = new TagNameFilter(FILTERS[0]);
			List<String> kjggList = new ArrayList<String>();
			NodeList nodeList = parser.parse(filter);
			ListNode realNode = null;
			for (int i = 0; i < nodeList.size(); i++) {
				ListNode node = nodeList.elementAt(i);
				String info = node.getText();
				if (info.indexOf(KEY_WORD) > -1) {
					nodeList = node.getChildren();
					realNode = node;
					break;
				}
			}
			TableTag tableTag = (TableTag) realNode;
			TableRow[] tableRows = tableTag.getRows();
			for (TableRow tableRow : tableRows) {
				String rowText = tableRow.getStringText();
				rowText = rowText.replace("<FONT color=#ff0000>", "");
				rowText = rowText.replace("<TD height=31>", "");
				rowText = rowText.replace("<TD height=46>", "");
				rowText = rowText.replace("<BR>", "");
				rowText = rowText.replace("</FONT>", "");
				rowText = rowText.replace("</P>", "");
				rowText = rowText.replace("<TD>", "");
				rowText = rowText.replace("</TD>", "");
				rowText = rowText.replace("&nbsp;", "");
				rowText = rowText.replace("<P style=\"MARGIN: 0px\">", "");
				int idx = rowText.indexOf("期");
				if (idx > 0) {
					kjggList.add(rowText);
				}
			}
		} catch (ParserException e) {
			e.printStackTrace();
		}
	}
}